package library.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import library.dao.BookDao;
import library.domain.Book;
import library.exception.DataException;
import library.server.Config;
import library.service.BookService;
import library.service.ServiceImpl;

public class BookServiceImpl extends ServiceImpl implements BookService {

    @Override
    public Book getBook(Integer identity, Boolean needContent) throws DataException {
        BookDao bookDao = daoManager.createDao(BookDao.class);
        Book book = bookDao.read(identity);
        if (!needContent) {
            return book;
        }
        File f = new File(String.format("%s/%d.%s.%s.%s", Config.getInstance().getValue("storage.path"), book.getId(), book.getAuthor(), book.getName(), book.getExtension()));
        Long size = f.length();
        book.setSize(size);
        int ind = f.getName().lastIndexOf(".");
        byte[] data = new byte[size.intValue()];
        try {
            FileInputStream fis = new FileInputStream(f);
            Byte[] d = new Byte[size.intValue()];
            fis.read(data);
            for (int i = 0; i < size.intValue(); ++i) {
                d[i] = data[i];
            }
            book.setContent(d);
        } catch (IOException ioe) {
            throw new DataException(ioe);
        }
        return book;
    }

    @Override
    public void newBook(Book book) throws DataException {
        BookDao bookDao = daoManager.createDao(BookDao.class);
        bookDao.create(book);
        try {
            FileOutputStream fos = new FileOutputStream(String.format("%s/%d.%s.%s.%s", Config.getInstance().getValue("storage.path"), book.getId(), book.getAuthor(), book.getName(), book.getExtension()));
            byte[] bytes = new byte[book.getSize().intValue()];
            for (int i = 0; i < book.getSize().intValue(); ++i) {
                bytes[i] = book.getContent()[i];
            }
            fos.write(bytes);
            fos.close();
        } catch (IOException ioe) {
            throw new DataException(ioe);
        }
    }

    @Override
    public List<Book> findByName(String name) throws DataException {
        BookDao bookDao = daoManager.createDao(BookDao.class);
        return bookDao.getByName(name);
    }

    @Override
    public List<Book> findByTag(String tag) throws DataException {
        BookDao bookDao = daoManager.createDao(BookDao.class);

        List<Book> books = bookDao.readAll();

        String tags[] = tag.split(";");

        for (String someTag : tags) {
            List<Book> temp = new LinkedList<Book>();
            forBook:
            for (Book book : books) {
                for (String someTagSomeBook : book.getTags()) {
                    if (someTagSomeBook.equalsIgnoreCase(someTag)) {
                        temp.add(book);
                        continue forBook;
                    }
                }
            }
            books = temp;
        }
        return books;
    }
}
